package com.phone.servlet.history;

import com.phone.DBHelper;
import com.phone.entity.Result;
import com.phone.exception.BaseException;
import com.phone.handle.HistoryHandle;
import com.phone.handle.UserHandle;
import com.phone.servlet.BaseServlet;
import org.hibernate.HibernateException;
import org.hibernate.Session;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by tisen on 2017/7/19.
 */
public class Save extends BaseServlet {
    private static final String REWARD_1 = "1"; //抽奖积分奖励
    private static final String REWARD_2 = "2"; //邀请奖励
    private static final String REWARD_3 = "3"; //下载应用奖励

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        setParamterRequest(request);
        Session session = DBHelper.getSession();
        session.beginTransaction();
        HistoryHandle historyHandle = new HistoryHandle(session);
        UserHandle userHandle = new UserHandle(session);
        Result result = null;
        try {
            String type = getParamterString("type",false);
            String ownerid = getParamterString("ownerid",false);
            String info = getParamterString("info",true);
            float money = getParamterFloat("money",true,0);
            int point = getParamterInt("point",true,0);

            switch (type) {
                case REWARD_1:
                    result = new Result<String>();
                    result.setObject(historyHandle.save(ownerid, point, money, info, 1));
                    result.setResult(SUCCESS);
                    result.setReason("save history successful");
                    userHandle.pointAndMoney(ownerid, point, money);
                    break;
                case REWARD_2:
                    result = new Result<String>();
                    result.setObject(historyHandle.save(ownerid, point, money, info, 2));
                    result.setResult(SUCCESS);
                    result.setReason("save history successful");
                    userHandle.pointAndMoney(ownerid, point, money);
                    break;
                case REWARD_3:
                    result = new Result<String>();
                    result.setObject(historyHandle.save(ownerid, point, money, info, 3));
                    result.setResult(SUCCESS);
                    result.setReason("save history successful");
                    userHandle.pointAndMoney(ownerid, point, money);
                    break;
                default:
                    result = new Result<String>();
                    result.setResult(ERROR);
                    result.setReason("undefined type");
                    break;
            }
            session.getTransaction().commit();
        } catch (HibernateException exception) {
            exception.printStackTrace();
            result.setReason("sql error");
            session.getTransaction().rollback();
        } catch (BaseException exception) {
            exception.printStackTrace();
            result.setReason(exception.getMessage());
            session.getTransaction().rollback();
        } catch (Exception e) {
            result.setReason(e.getMessage());
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen())
                session.close();
            out(response, result);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

}
